package com.chenu.gamscriptman.controller;

import com.chenu.gamscriptman.compoent.Global;
import com.chenu.gamscriptman.dao.UserDao;
import com.chenu.gamscriptman.domain.UserData;
import com.chenu.gamscriptman.domain.noindb.GameMap;
import com.chenu.gamscriptman.domain.noindb.Message;
import com.chenu.gamscriptman.domain.noindb.PageBean;
import com.chenu.gamscriptman.domain.noindb.Statistics;
import com.chenu.gamscriptman.service.UserDataTask;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import springfox.documentation.annotations.ApiIgnore;

import java.util.List;

/**
 * @author flyan
 * date 2019-12-29
 * @function 用户数据控制器，它用于处理用户数据任务完成需要视图的服务后返回的回复消息
 */
@Controller
@ApiIgnore
public class UserDataController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    UserDataTask userDataTask;

    @Autowired
    private RedisTemplate jsonRedisTemplate;

    /**
     * 用户数据管理/列出所有用户
     */
    @GetMapping("/userdatas")
    public String users(
            Model model
    ){
        // 放入分页数据和查询参数
        PageBean<UserData> userdata_page = (PageBean<UserData>) jsonRedisTemplate.opsForValue().get(Global.REDIS_USERDATA_PAGES);
        model.addAttribute(Global.REDIS_USERDATA_PAGES, userdata_page);
        UserData search_userdata = (UserData) jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_USERDATA);
        model.addAttribute(Global.REDIS_SEARCH_USERDATA, search_userdata);
        /* 放入统计信息 */
        Statistics userdata_statistics = (Statistics) jsonRedisTemplate.opsForValue().get(Global.REDIS_USERDATA_STATISTICS);
        model.addAttribute(Global.REDIS_USERDATA_STATISTICS, userdata_statistics);
        /* 所有的游戏名称 */
        model.addAttribute(Global.REDIS_SEARCH_GAME_NAME_LIST,
                (List<String>)jsonRedisTemplate.opsForValue().get(Global.REDIS_SEARCH_GAME_NAME_LIST));
        /* 很重要的一步，放入游戏配置映射 */
        model.addAttribute(Global.REDIS_USERDATA_MAP,
                (GameMap)jsonRedisTemplate.opsForValue().get(Global.REDIS_USERDATA_MAP));

        return "userdata/list";
    }

}
